#ifndef __DYNAMICARRAY_H
#define __DYNAMICARRAY_H

#include <iostream>
#include <memory>

template<typename T>
class DynamicArray
{
public:
    DynamicArray()
    {
        capacity = 15;
        a = new T[capacity];
        size = 0;
    }
    ~DynamicArray()
    {
        delete []a;
        size = 0;
        capacity = 0;
    }
    void Clear();

    void InsertTail(const T& element);

    template <typename U>
    friend std::ostream& operator<<(std::ostream& os,const DynamicArray<U>& element);
protected:
    // T *a;
    std::shared_ptr<T> a;
    int size;
    int capacity;
};


template <typename T>
void DynamicArray<T>::InsertTail(const T &element)
{
    if(size >= capacity)
    {
        capacity *= 2;
        T *newA = new T[capacity];
        for(int i = 0; i < size; i++)
        {
            newA[i] = a[i];
        }
        delete []a;
        a = newA;
    }
    a[size++] = element;
}

template <typename T>
std::ostream& operator<<(std::ostream& os,const DynamicArray<T>& element)
{
    for(int i = 0; i < element.size; i++)
    {
        os<<element.a[i]<<" ";
    }
    return os;
}

template <typename T>
void DynamicArray<T>::Clear()
{
    delete []a;
    size = 0;
    capacity = 15;
    a = new T[capacity];
}















#endif